﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Microsoft.Office.Interop.Word;
using System.Windows.Media;

namespace SanctusSoftware.Music.ChordSheetEditor.Word
{
  public class WdColorColorConverter : IValueConverter
  {

    /*
   http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdcolor%28office.11%29.aspx?ppud=4

   Setting colors that are not in the WdColor enumeration Edit

   You can use the values specified in the enumeration when you need to use a WdColor or you can also pass a specific 24-bit color value.  To calculate the bit color value use the following exquation:

   value = (blue * 65536) + (green * 256) + red

   This is the same value you would get from the VB RGB function.  To use the value you will need to cast the value as a WdColor.  For example what ever you are setting the color of you would set the value with the following value if you are using C#:

   ((Word.WdColor) <calculated value>)
   */


    //Turns out, Word's wdColor uses BGR instead of RGB. Plus what it calls things like "Dark Green" are different shades of green than WPF Colors.

    //This code sample shows it all:

    //      if (comboBox1.SelectedItem != null)
    //  {
    //    Word.WdColor selectedWdColor = (Word.WdColor)System.Enum.Parse(typeof(Word.WdColor), (string)comboBox1.SelectedItem);

    //    int value = selectedWdColor.GetHashCode();
    //    string hex = value.ToString("X").PadLeft(6, '0');

    //    string redHex = hex.Substring(4, 2);
    //    string greenHex = hex.Substring(2, 2);
    //    string blueHex = hex.Substring(0, 2); ;

    //    string correctedHex = redHex + greenHex + blueHex;

    //    byte redByte = byte.Parse(redHex, System.Globalization.NumberStyles.AllowHexSpecifier);
    //    byte greenByte = byte.Parse(greenHex, System.Globalization.NumberStyles.AllowHexSpecifier);
    //    byte blueByte = byte.Parse(blueHex, System.Globalization.NumberStyles.AllowHexSpecifier);

    //    Color c = Color.FromRgb(redByte, greenByte , blueByte);
    //    this.Background = new SolidColorBrush(c);

    //  }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

      if (!(value is WdColor))
      {
        return Colors.Black;
      }
      else if ((WdColor)value == WdColor.wdColorAutomatic)
      {
        return Colors.Black;
      }
      else
      {

        int intValue = ((WdColor)value).GetHashCode();
        string hexValue = intValue.ToString("X").PadLeft(6, '0');

        string redHex = hexValue.Substring(4, 2);
        string greenHex = hexValue.Substring(2, 2);
        string blueHex = hexValue.Substring(0, 2);

        byte redByte = byte.Parse(redHex, System.Globalization.NumberStyles.AllowHexSpecifier);
        byte greenByte = byte.Parse(greenHex, System.Globalization.NumberStyles.AllowHexSpecifier);
        byte blueByte = byte.Parse(blueHex, System.Globalization.NumberStyles.AllowHexSpecifier);

        Color c = Color.FromRgb(redByte, greenByte, blueByte);
        return c;

      }
    }



    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}
